<?php

namespace common\models\DAO;

use common\models\DAO\ArticleType;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%article}}".
 *
 * @property string $id
 * @property string $title
 * @property string $thumb
 * @property integer $adminID
 * @property string $typeID
 * @property integer $status
 * @property integer $isDelete
 * @property string $createTime
 * @property string $updateTime
 * @property integer $sort
 * @property integer $click
 * @property integer $description
 */
class Article extends \yii\db\ActiveRecord
{
    //文章内容属性，附表用
    public $content = '';

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%article}}';
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键id',
            'title' => '文章标题',
            'thumb' => '缩略图',
            'adminID' => '管理员ID',
            'typeID' => '文章分类',
            'status' => '状态',
            'isDelete' => '是否删除',
            'createTime' => '创建时间',
            'updateTime' => '更新时间',
            'sort' => '排序',
            'description' => '描述',
            'content' => '内容',
            'click' => '点击量',
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'typeID'], 'required'],
            [['typeID', 'status', 'isDelete', 'createTime', 'updateTime', 'sort'], 'integer'],
            [['typeID'],  'integer','min' => 1],
            [['sort'], 'default', 'value' => 0],
            [['status'], 'default', 'value' => 0],
            [['title'], 'string', 'min' => 2, 'max' => 30],
            [['thumb'], 'string', 'max' => 255],
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [],
            'add' => ['title', 'thumb', 'description', 'typeID', 'status', 'createTime', 'updateTime', 'sort', 'content'],
            'edit' => ['title', 'thumb', 'description', 'typeID', 'status', 'updateTime', 'sort', 'content'],
            'del' => ['isDelete']
        ];
    }

    public function beforeSave($insert)
    {
        if($insert){
            $this->createTime = time();
            $this->adminID = \Yii::$app->session->get('ADMIN_ID');
            $this->click = mt_rand(50, 200);
        }
        $this->updateTime = time();
        return parent::beforeSave($insert);
    }

    public function afterFind()
    {
        $articleInfo = ArticleInfo::findOne(['articleID' =>$this->id]);
        if($articleInfo){
            $this->content = $articleInfo->content;
        }else{
            $this->content = '';
        }

    }

    public function getAdmin(){
        return $this->hasOne(Admin::className(), ['id' => 'adminID']);
    }

    public function getArticleType(){
        return $this->hasOne(ArticleType::className(), ['id' => 'typeID']);
    }

    public function getArticleInfo(){
        return $this->hasOne(ArticleInfo::className(), ['articleID' => 'id']);
    }
    
    public static function getSimpleList($where = '')
    {
        $query = self::find()->select('id, title');
        if(!empty($where))
            $query->where($where);
        $all = $query->asArray()->all();
        if(!empty($all)){
            return ArrayHelper::map($all, 'id', 'list');
        }
    }
}
